<!-- 武器 -->
<template>
  <van-form ref="form">
    <!-- 武器 -->
    <div class="more">
      <p class="bb">
        <img src="@/assets/img/gm/4.png" alt="" />
        武器
      </p>
      <van-field
        v-model="form.search"
        name="search"
        label="搜索"
        placeholder="武器ID或名称"
      >
      </van-field>
      <van-field
        v-model="form.name"
        name="name"
        label="武器名称"
        placeholder="请选择武器"
        readonly
        @click="openPicket"
        :rules="[{ required: true, message: '请选择武器' }]"
      />
      <van-popup v-model="showPicker" round position="bottom">
        <van-picker
          show-toolbar
          :columns="weaponList"
          @cancel="showPicker = false"
          @confirm="onConfirm"
        >
          <template #option="option">
            <div style="display: flex; align-items: center">
              <div>{{ option.id }}---</div>
              <div>{{ option.name }}</div>
            </div>
          </template>
        </van-picker>
      </van-popup>
      <van-field name="tplevel" label="突破等级">
        <template #input>
          <div class="qm-slider-box">
            <div class="qm-slider">
              <van-slider min="0" max="6" v-model="form.tplevel" />
            </div>
            <div class="qm-title">{{ form.tplevel }}</div>
          </div>
        </template>
      </van-field>
      <van-field name="level" label="武器等级">
        <template #input>
          <div class="qm-slider-box">
            <div class="qm-slider">
              <van-slider min="1" max="90" v-model="form.level" />
            </div>
            <div class="qm-title">{{ form.level }}</div>
          </div>
        </template>
      </van-field>
      <van-field name="jllevel" label="精炼等级">
        <template #input>
          <div class="qm-slider-box">
            <div class="qm-slider">
              <van-slider min="1" max="5" v-model="form.jllevel" />
            </div>
            <div class="qm-title">{{ form.jllevel }}</div>
          </div>
        </template>
      </van-field>
      <button type="button" class="btn btn-01 btn-block" @click="weaponFun()">
        添加
      </button>
    </div>
  </van-form>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import WeaponApi from '@/api/gms/weapon'
export default {
  //import引入的组件需要注入到对象中才能使用
  name: 'weapon-gm',
  components: {},
  data() {
    //这里存放数据
    return {
      allWeaponList: [],
      showPicker: false,
      weaponList: [],
      form: {
        search: null,
        name: null,
        id: null,
        tplevel: 0,
        jllevel: 1,
        level: 1
      }
    }
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    //打开选择武器面板
    openPicket() {
      if (this.form.search) {
        const res = []
        this.allWeaponList.forEach((items) => {
          if (
            items.id.indexOf(this.form.search) != -1 ||
            items.name.indexOf(this.form.search) != -1
          ) {
            res.push(items)
          }
        })
        this.weaponList = res
      } else {
        this.weaponList = this.allWeaponList
      }
      if (this.weaponList.length <= 0) {
        this.$toast('没有搜索到相关武器')
        return
      }
      this.showPicker = true
    },
    //选择武器事件
    onConfirm(value) {
      this.form.name = value.name
      this.form.id = value.id
      this.showPicker = false
    },
    //添加武器
    async weaponFun() {
      // 校验
      await this.$refs.form.validate('name')
      const res = await WeaponApi.addWeapon(this.form)
      if (res && res.code == 200) {
        this.$toast.success('success')
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    const res = await WeaponApi.getAllWeapon()
    if (res && res.code == 200) {
      this.allWeaponList = res.data
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeUnmount() {}, //生命周期 - 销毁之前
  unmounted() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped lang="less">
.van-cell {
  padding: 20px;
  margin-bottom: 20px;
  &::after {
    left: 0;
    right: 0;
  }
}

.qm-slider-box {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  .qm-slider {
    width: 80%;
    height: 100%;
    display: flex;
    justify-content: center;
    margin-right: 20px;
  }
  .qm-title {
    width: 25px;
    height: 25px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
    // color: #ddd;
    background: #ddd;
  }
}
.more {
  margin: 0 10px;
  padding: 0px 0px 15px 0px;
  background-color: #fff;
  border-radius: @radius;
  margin-bottom: 20px;
  p {
    font-size: 16px;
    padding: 10px 15px;
    margin: 0;
    border-bottom: 1px solid #00000014;
    display: flex;
    align-items: center;
    img {
      width: 30px;
      height: 30px;
      margin-right: 5px;
    }
  }
}
.btn {
  padding: 6px 12px;
  font-size: 14px;
  text-align: center;
  white-space: nowrap;
  border: 1px solid transparent;
  border-radius: 4px;
  font-weight: 400;
  vertical-align: middle;
  cursor: pointer;
  user-select: none;
}

.btn-block {
  display: block;
}
.btn-01 {
  width: 90%;
  margin-left: 5%;
  height: 40px;
  color: #fff;
  font-weight: 700;
  background-color: #096;
  border: 1px dashed #ff0;
}

.btn-02 {
  width: 40%;
  height: 40px;
  color: #fff;
  font-weight: 700;
  background-color: #096;
  border: 1px dashed #ff0;
}

.btn-g {
  display: flex;
  justify-content: space-evenly;
}
</style>
